package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.com.api.push.AndroidToSingleDevice;
import cn.com.api.push.IOSToSingleDevice;
import cn.com.api.push.Keyandsecretkey;
import cn.com.api.resultpage.InitPushPage;
import cn.com.api.service.ApiPushService;
import cn.com.util.*;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;

@Service
public class ApiPushServiceImpl implements ApiPushService {

	@Override
	public void sendOrderInfoToShop(InitPushPage pushPage) {
		Integer deviceType = pushPage.getDeviceType();// 0-IOS 1-Android
		String deviceId = pushPage.getDeviceId();
		String orderIds = pushPage.getOrderIds();
		String alert = pushPage.getAlert();
		String title = pushPage.getTitle();
		String appkey = Keyandsecretkey.SHOPER_APPKEY;
		String secretkey = Keyandsecretkey.SHOPER_SECRETKEY;
		String message = pushPage.getMessage();

		List<String> registrationIds = new ArrayList<String>();
		Map<String, String> params = pushPage.getParams();

		// 订单号，每次只发一个商铺，因为一个客户在一次购物只会在一家店产生一个订单
		registrationIds.add(deviceId);

		try {
			if (!Util.isNull(deviceType)) {
//					if(0==deviceType){//IOS--配合IOS测试
//						IOSToSingleDevice.getInstance().iostosingledevice(appkey, secretkey, alert, params, registrationIds);
//					}else{
						AndroidToSingleDevice.getInstance().androidtosingleDevice(
								appkey, secretkey, alert, title, params,
								registrationIds);
//					}
					
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void sendOrderInfoToUser(InitPushPage pushPage) {
		Integer deviceType = pushPage.getDeviceType();// 0-IOS 1-Android
		String deviceId = pushPage.getDeviceId();
		String orderIds = pushPage.getOrderIds();
		String alert = pushPage.getAlert();
		String title = pushPage.getTitle();
		String appkey = Keyandsecretkey.USER_APPKEY;
		String secretkey = Keyandsecretkey.USER_SECRETKEY;
		String message = pushPage.getMessage();

		List<String> registrationIds = new ArrayList<String>();
		Map<String, String> params = pushPage.getParams();

		// 订单号，每次只发一个商铺，因为一个客户在一次购物只会在一家店产生一个订单
		registrationIds.add(deviceId);

		try {
			if (!Util.isNull(deviceType)) {
//				if(0==deviceType){//IOS
//					IOSToSingleDevice.getInstance().iostosingledevice(appkey, secretkey, alert, params, registrationIds);
//				}else{
					AndroidToSingleDevice.getInstance().androidtosingleDevice(
							appkey, secretkey, alert, title, params,
							registrationIds);
//				}
				
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void sendOrderInfoToSender(InitPushPage pushPage) {
		Integer deviceType = pushPage.getDeviceType();// 0-IOS 1-Android
		String deviceId = pushPage.getDeviceId();
		String orderIds = pushPage.getOrderIds();
		String alert = pushPage.getAlert();
		String title = pushPage.getTitle();
		String appkey = Keyandsecretkey.SEND_APPKEY;
		String secretkey = Keyandsecretkey.SEND_SECRETKEY;
		String message = pushPage.getMessage();

		List<String> registrationIds = new ArrayList<String>();
		Map<String, String> params = pushPage.getParams();

		// 订单号，每次只发一个商铺，因为一个客户在一次购物只会在一家店产生一个订单
		registrationIds.add(deviceId);

		try {
			if (!Util.isNull(deviceType)) {
//				if(0==deviceType){//IOS
//					IOSToSingleDevice.getInstance().iostosingledevice(appkey, secretkey, alert, params, registrationIds);
//				}else{
					AndroidToSingleDevice.getInstance().androidtosingleDevice(
							appkey, secretkey, alert, title, params,
							registrationIds);
//				}
				
			}
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (APIRequestException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
